<?php

	class cmd_goods extends mdl_goods
	{
		function getFieldByProductId($product_id, $aField=array('*'))
		{
			return $this->db->selectrow("SELECT ".implode(",", $aField)." FROM sdb_products WHERE product_id='{$product_id}'");
		}
		/*
		//保存(新增/编辑)商品,物品,会员价格
		function save(&$aData)
		{
			if(empty($aData['name'])){
				return false;
				exit;
			}
			$products = $aData['products'];
			$aData['pdt_desc'] = (is_array($products) ? array() : '');
			$aData['spec'] = (is_array($products) ? $aData['spec'] : '');
			$aData['uptime'] = time();
			$aData['cost'] = floatval($aData['cost']);
			$aData['last_modify'] = time();
			$aData['weight'] = (trim($aData['weight']) ? $aData['weight'] : 0);
			if(empty($aData['bn'])) $aData['bn'] = strtoupper(uniqid('g'));
			else{
				if($this->checkGoodsValid($aData)){
					trigger_error('保存失败：货号重复！',E_USER_ERROR);
					return false;
				}
			}
			unset($aData['products']);
			$aData['price'] = floatval($aData['price']);
			if(!$aData['mktprice']){
				$oMath = $this->system->loadModel('system/math');
				if($this->system->getConf('site.show_mark_price')){
					if($this->system->getConf('site.market_price') == '1')
						$aData['mktprice'] = $this->system->getConf('site.market_rate')* $oMath->getOperationNumber( $aData['price'] );
					if($this->system->getConf('site.market_price') == '2')
						$aData['mktprice'] = $this->system->getConf('site.market_rate')+$oMath->getOperationNumber( $aData['price'] );
				}
			}

			if($aData['brand_id'] && !$aData['brand']){
				$objBrand = $this->system->loadModel('goods/brand');
				$aBrand = $objBrand->getFieldById($aData['brand_id'], array('brand_name'));
				$aData['brand'] = addslashes($aBrand['brand_name']);
			}
			
			if( trim($aData['score']) === '' )
				$aData['score'] = 0;
			
			if($aData['goods_id']){
				$rs = $this->db->query('SELECT * FROM sdb_goods WHERE goods_id='.$aData['goods_id']);
				$sql = $this->db->GetUpdateSQL($rs, $aData);
				if($sql && !$this->db->exec($sql)){
					trigger_error('SQL Error:'.$sql,E_USER_NOTICE);
					return false;
				}
			}else{
				$aData['d_order'] = 50;
				$rs = $this->db->query('SELECT * FROM sdb_goods WHERE 0=1');
				$sql = $this->db->GetInsertSQL($rs, $aData);
				if($sql && !$this->db->exec($sql)){
					trigger_error('SQL Error:'.$sql,E_USER_NOTICE);
					return false;
				}
				$aData['goods_id'] = $this->db->lastInsertId();
				$this->db->exec('UPDATE sdb_goods SET p_order='.$aData['goods_id'].' WHERE goods_id='.$aData['goods_id']);
				$pData['uptime'] = time();
				$pData['last_modify'] = time();

				$status = $this->system->loadModel('system/status');
				$status->add('GOODS_ADD');
			}
			$this->setGoodsMemo($aData['goods_id'], 'seo_info', $aData['seo']);

			$pData['goods_id'] = $aData['goods_id'];
			$pData['cost'] = $aData['cost'];
			$pData['name'] = $aData['name'];
			$pData['weight'] = $aData['weight'];
			$pData['unit'] = $aData['unit'];

			//按规格插入货品
			$aMprice=array();
			$aPdt=array();

			$specIndexData = array();
			if(is_array($products)){    //多物品商品
				$iLoop = 0;
				$aIndex = array();
				$aPdtDesc['store'] = 0;
				foreach($products as $k => $aProduct){
					if(empty($aProduct['bn'])){
						$aProduct['bn'] = $this->getPdtBn($aData['bn'], $products);        //生成物品编号
						$products[$k]['bn'] = $aProduct['bn'];
					}
					$aProduct = array_merge($pData, $aProduct);
					if($newPid = $this->addProducts($aProduct)){

						$newProductId = $this->db->selectrow('SELECT product_id FROM sdb_products WHERE bn = "'.$newPid.'"');
						//插入规格索引表
						foreach( $aProduct['props']['spec_private_value_id'] as $pubSpecId => $pSpecId ){
							$specIndexData[] = array(
									'type_id'=>$aData['type_id'],
									'spec_id'=>$pubSpecId,
									'spec_value_id'=>$aProduct['props']['spec_value_id'][$pubSpecId],
									'goods_id'=>$aProduct['goods_id'],
									'product_id'=>$newProductId['product_id']
							);
						}

						$aPdt[] = $newPid;
						if($aPdtDesc['store'] === '' || trim($aProduct['store']) === ''){
							$aPdtDesc['store'] = '';
						}else{
							$aPdtDesc['store'] += $aProduct['store'];
						}
						if(is_array($aProduct['mprice'])){
							$tmpPrice['goods_id'] = $aData['goods_id'];
							$tmpPrice['product_id'] = $newPid;
							$tmpPrice['price'] = $aProduct['mprice'];
							$aMprice[] = $tmpPrice;
							$tmpArr[$newPid] = $iLoop;
							$iLoop++;
						}
						$aIndex['product_id'][] = $newPid;
						$aIndex['spec'][] = $aProduct['props']['spec'];
					}
					$aProps[] = $aProduct['props'];
				}

				$usedPdt = array();
				foreach($this->db->select('select product_id,pdt_desc,bn from sdb_products where bn in(\''.implode('\',\'',$aPdt).'\') and goods_id='.$aData['goods_id']) as $r){
					$usedPdt[] = $r['product_id'];
					$aMprice[$tmpArr[$r['bn']]]['product_id'] = $r['product_id'];
					$aPdtDesc['pdt_desc'][$r['product_id']] = $r['pdt_desc'];
					
					// alone add start 
					if (strstr($r['pdt_desc'], '静态模板'))
					{
						$aPdtDesc['ppt_desc'][$r['product_id']] = $aData['static_ppt'];
					}
					if (strstr($r['pdt_desc'], '动态模板'))
					{
						$aPdtDesc['ppt_desc'][$r['product_id']] = $aData['dynamic_ppt'];
					}
					//alone add end 
					foreach($aIndex['product_id'] as $k => $v){
						if($v == $r['bn']){
							$aIndex['product_id'][$k] = $r['product_id'];
							break;
						}
					}
				}
				//error_log(print_r($aPdtDesc,true),3,__FILE__.'.'.date('Y-m-d H-i-s').'.'.__FUNCTION__.'.'.__LINE__.'.aPdtDesc.log');	//alone
				$rs = $this->db->query('SELECT * FROM sdb_goods WHERE goods_id='.$aData['goods_id']);
				$sql = $this->db->GetUpdateSQL($rs, $aPdtDesc);
				//error_log(print_r($sql,true),3,__FILE__.'.'.date('Y-m-d H-i-s').'.'.__FUNCTION__.'.'.__LINE__.'.sql.log');	//alone
				if($sql) $this->db->exec($sql);

			}else{    //单物品商品
				$pData['bn'] = ($aData['product_bn']?$aData['product_bn']:$aData['bn']);
				$pData['store'] = $aData['store'];
				$pData['price'] = $aData['price'];
				$pData['props'] = $aData['props'];
				$newPid = $this->addProducts($pData);

				if(is_array($aData['mprice'])){
					$aMprice[0]['goods_id'] = $aData['goods_id'];
					$aMprice[0]['product_id'] = $newPid;
					$aMprice[0]['price'] = $aData['mprice'];
				}
				$aPdt[] = $newPid;
				$usedPdt = array();
				foreach($this->db->select('select product_id,pdt_desc from sdb_products where bn in(\''.implode('\',\'',$aPdt).'\') and goods_id='.$aData['goods_id']) as $r){
					$usedPdt[] = $r['product_id'];
					$aMprice[0]['product_id'] = $r['product_id'];
				}
			}
			$this->updateGoodsIndex( $aData['goods_id'], $specIndexData);
			$this->removePdt($aData['goods_id'], $usedPdt);

			//处理配件
			foreach($aData['adjunct']['name'] as $key => $name){
				$aItem['name'] = $name;
				$aItem['type'] = $aData['adjunct']['type'][$key];
				$aItem['min_num'] = $aData['adjunct']['min_num'][$key];
				$aItem['max_num'] = $aData['adjunct']['max_num'][$key];
				$aItem['set_price'] = $aData['adjunct']['set_price'][$key];
				$aItem['price'] = $aData['adjunct']['price'][$key];
				if($aItem['type'] == 'goods') $aItem['items']['product_id'] = $aData['adjunct']['items'][$key];
				else $aItem['items'] = $aData['adjunct']['items'][$key];//.'&dis_goods[]='.$aData['goods_id']
				$aAdj[] = $aItem;
			}
			$this->setGoodsMemo($aData['goods_id'], 'adjunct', $aAdj);

			if($aData['updateMprice']){
				$sql = 'DELETE FROM sdb_goods_lv_price WHERE goods_id='.$aData['goods_id'];
				$this->db->exec($sql);
			}else{
				if(count($aMprice))
					$this->addMemberPrice($aMprice);
			}
			if(!$status){
				$status = $this->system->loadModel('system/status');
			}
			$status->count_goods_online();
			$status->count_goods_hidden();
			$status->count_galert();

			return $aData['goods_id'];
		}
		*/
	}

?>